#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <time.h>
#define SERVPORT 9998
#define MODULE_LOGIN_REQ 0x00006001
#define HEARTBEAT_LINK_REQ 0x00006005
struct header
{
	unsigned int version;
	unsigned int command_id;
	unsigned int length;
	unsigned int sequence;
	unsigned int status;
};
int
main(int argc, char *argv[])
{
	int sockfd;
	struct hostent *host;
	struct sockaddr_in serv_addr;
	if(argc < 2)
	{
		argv[1]="127.0.0.1";
	}
	if((host = gethostbyname(argv[1])) == NULL)
	{
		herror("gethostbyname error!");
		return -1;
	}
	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket error!");
		return -1;
	}

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(SERVPORT);
	serv_addr.sin_addr =*((struct in_addr*)host->h_addr);
	bzero(&(serv_addr.sin_zero),8);
	if(-1 == connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr)))
	{
		perror("connect error");
		return -1;
	}
	char buf[512];
	memset(buf, 0, 512);
	struct header h;
	unsigned int seq = 0;
	while(1)
	{
		memset(&h, 0, sizeof(struct header));	
		h.version = 0x00020001;
		h.length = 0;
		h.command_id = HEARTBEAT_LINK_REQ;
		h.sequence = seq;
		h.status = 0;
		memcpy(buf, &h, sizeof(struct header));
		if(-1 == send(sockfd, &buf, sizeof(struct header), 0))
		{
			perror("send error");
			break;
		}
		memset(buf,0,512);

		if((recv(sockfd, &buf, sizeof(struct header),0)) <= 0)
		{
			printf("recv error:%s\n",strerror(errno));
			break;
		}
		memset(&h, 0, sizeof(struct header));	
		memcpy(&h, buf, sizeof(struct header));
		printf("%x\n",h.command_id);
		seq++;
		memset(buf,0,512);
		sleep(1);
	}
	close(sockfd);
	return 0;
}
